﻿using Api.Application.Command.Leave;
using Domain;
using ErrorCode;
using Infrastructure;
using MediatR;

namespace Api.Application.Handler.Leave
{
    public class UpdateStatuHandler : IRequestHandler<UpdateStatuCommand, ApiResult<int>>
    {
        private readonly IBase<LeaveModel> leaveRepo;
        private readonly IBase<UserModel> userRepo;
        private readonly IBase<UserRoleModel> userRoleRepo;
        private readonly IBase<RoleModel> roleRepo;

        public UpdateStatuHandler(IBase<LeaveModel> leaveRepo,IBase<UserModel> userRepo,IBase<UserRoleModel> userRoleRepo,IBase<RoleModel> roleRepo)
        {
            this.leaveRepo = leaveRepo;
            this.userRepo = userRepo;
            this.userRoleRepo = userRoleRepo;
            this.roleRepo = roleRepo;
        }
        public async Task<ApiResult<int>> Handle(UpdateStatuCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result=new ApiResult<int>();
            var list=leaveRepo.GetAll().Where(x=>x.Id==request.Id).FirstOrDefault();
            if (request.ApprovalState == false)
            {
                if (string.IsNullOrEmpty(request.ApprocalYuan))
                {
                    result.Data = 0;
                    result.Code = ApiEnum.Fail;
                    result.Msg = "请填写不通过原因";
                }
                list.ApprovalState = false;
                list.ApprocalYuan = request.ApprocalYuan;
            }
            else if (request.ApprovalState == true)
            {
                list.ApprovalState= true;
                list.ApprocalYuan = null;
            }
            await leaveRepo.Update(list);
            result.Code = ApiEnum.OK;
            result.Data = 1;
            result.Msg = "审批成功";
            return await Task.FromResult(result);
        }
    }
}
